package com.szy.web.servlet;

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.jspsmart.SmartFile;
import com.jspsmart.SmartRequest;
import com.jspsmart.SmartUpload;
import com.szy.web.dao.NewsDAO;
import com.szy.web.util.TextUtility;

public class addNewsServlet extends HttpServlet  implements Servlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html;charset=UTF-8");
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		//登陆权限设置
		if ( req.getSession().getAttribute("Login") != null) 
		{
				String message = (String) req.getSession().getAttribute("Login");
				if (!message.equals("true"))
				{
					 //如果登录标志不为true则跳转回登录页面
					resp.sendRedirect("index.jsp");
				 	return;
				}
				else
					req.getSession().setAttribute("Login","true");

		 }
		else
		{
			 resp.sendRedirect("index.jsp");
			 return;
		}
		ServletConfig config = getServletConfig();
		SmartUpload su= new SmartUpload();
		try {
			su.initialize(config, req, resp);
			su.setAllowedFilesList("gif,jpg,jpeg,png");
			su.setMaxFileSize(1024*1024*10);
			//上传
			su.upload();
			Date date = new Date();
			String filename = "";
			SmartFile file = su.getFiles().getFile(0);
			if( !file.isMissing() )
			{
				filename = file.getFileName();
				@SuppressWarnings("deprecation")
				String yearString = date.getYear()+"";
				@SuppressWarnings("deprecation")
				String mountString = date.getMonth()+"";
				@SuppressWarnings("deprecation")
				String dayString = date.getDay()+"";
				@SuppressWarnings("deprecation")
				String hourString = date.getHours()+"";
				@SuppressWarnings("deprecation")
				String miniteString = date.getMinutes()+"";
				@SuppressWarnings("deprecation")
				String secString = date.getSeconds()+"";
				filename =yearString  +mountString+dayString+hourString+miniteString+secString+ filename;
				System.out.println(filename);
				file.saveAs("PIC/"+filename,SmartFile.SAVEAS_VIRTUAL);
			}
			SmartRequest request = su.getRequest();		
			String title = null;
			title = request.getParameter ( "title" );
			String digest =request.getParameter ( "digest" );
			String bodytmp = request.getParameter ( "detail" );
			if ( title == "" || digest == null || bodytmp == null )
			{
				System.out.println(" here is null");
				//req.setAttribute("AddNews","Failed");
				resp.sendRedirect("AddNewsMain.jsp");
				return;
			}
			System.out.println("title = "+title);
			String cidtmp  =request.getParameter ( "Category" );
			int cid = TextUtility.String2Int(cidtmp);

			String body = TextUtility.ToHtml(bodytmp);
			String source =request.getParameter ( "source" );
			System.out.println( source );
			String ptime = "2013-4-29 10:20:00" ;
			String imgsrc = filename;
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			ptime = df.format( date );// new Date()为获取当前系统时间
			
			try {
				NewsDAO newsdao = new NewsDAO();
				newsdao.addNews( cid, title, digest, body, source, ptime, imgsrc);
				System.out.println("数据库成功");
			} 
			catch (ClassNotFoundException e) {
				System.out.println("数据库失败");
				req.setAttribute("AddNews","Failed");
				resp.sendRedirect("AddNewsMain.jsp");
				e.printStackTrace();
			} 
			catch (SQLException e) 
			{
				System.out.println("数据库失败");
				req.setAttribute("AddNews","Failed");
				resp.sendRedirect("AddNewsMain.jsp");
				e.printStackTrace();
			}
			System.out.println("成功");
			req.setAttribute("AddNews","Success");
			resp.sendRedirect("AddNewsMain.jsp");
		} catch (Exception e) 
		{
			System.out.println("上传失败");
			req.setAttribute("AddNews","Failed");
			resp.sendRedirect("AddNewsMain.jsp");
		}
	}
}
